﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GmMessageBox.h"
#include "GbmpNew.h"
#include <QTimer>
#include "TranslatorManager.h"
#include "GmStyleSheetHelper.h"
#include "qevent.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

GmMessageBox::GmMessageBox(QWidget *parent)
    : QDialog(parent, Qt::FramelessWindowHint | Qt::WindowSystemMenuHint
        | Qt::WindowMinMaxButtonsHint)
    , m_clickedButton(gcmp::UiCommonDialog::ButtonType::Invalid)
{
    ui.setupUi(this);
    this->resize(480, 320);
    std::wstring styleSheetStr = GmStyleSheetHelper::Instance()->GetCurrentStyleData();
    setStyleSheet(QString::fromStdWString(styleSheetStr));
    ui.gbmp_title->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    ui.gbmp_title->setIconVisible(false);
    ui.gbmp_title->setMaxButtonVisible(false);
    ui.gbmp_title->setMinButtonVisible(false);
    ui.gbmp_title->setText(QString::fromStdWString(GBMP_TR(L"信息框")));
    setProperty("use_gbmp_title", true);
    connect(ui.gbmp_title, &GmTitleBar::closeRequested, this, &GmMessageBox::reject);
    connect(ui.gbmp_title, &GmTitleBar::moveRequested, this, &GmMessageBox::onMoveRequested);

    installEventFilter(this);

    QObject::connect(ui.pushButtonOK, SIGNAL(clicked()), this, SLOT(OnButtonClick()));
    QObject::connect(ui.pushButtonYes, SIGNAL(clicked()), this, SLOT(OnButtonClick()));
    QObject::connect(ui.pushButtonCancel, SIGNAL(clicked()), this, SLOT(OnButtonClick()));
    QObject::connect(ui.pushButtonNo, SIGNAL(clicked()), this, SLOT(OnButtonClick()));
    QObject::connect(ui.pushButtonIgnore, SIGNAL(clicked()), this, SLOT(OnButtonClick()));
    QObject::connect(ui.pushButtonIgnoreOnce, SIGNAL(clicked()), this, SLOT(OnButtonClick()));
    QObject::connect(ui.pushButtonIgnoreAll, SIGNAL(clicked()), this, SLOT(OnButtonClick()));

    // 这里的次序必须与EnButtonType里的次序相一致
    m_allButtons.push_back(ui.pushButtonOK);
    m_allButtons.push_back(ui.pushButtonYes);
    m_allButtons.push_back(ui.pushButtonCancel);
    m_allButtons.push_back(ui.pushButtonNo);
    m_allButtons.push_back(ui.pushButtonIgnore);
    m_allButtons.push_back(ui.pushButtonIgnoreOnce);
    m_allButtons.push_back(ui.pushButtonIgnoreAll);

    // 默认仅留一个“确定”按钮
    for(int loop = 1; loop < (int)m_allButtons.size(); ++loop)
    {
        m_allButtons[loop]->setHidden(true);
    }

    for (int loop = 0; loop < (int)m_allButtons.size() - 1; ++loop)
    {
        QWidget::setTabOrder(m_allButtons[loop], m_allButtons[loop + 1]);
    }
}

GmMessageBox::~GmMessageBox()
{

}

void GmMessageBox::SetTitle(const std::wstring& title)
{
    ui.gbmp_title->setText(QString::fromStdWString(title));
}

void GmMessageBox::SetMessage(const std::wstring& title)
{
    ui.textEditMessage->setText(QString::fromStdWString(title));
}

void GmMessageBox::SetAutoCloseAfterMiliSeconds(int miliSeconds)
{
    if(miliSeconds > 0)
    {
        m_upIdleTimer = NEW_AS_OWNER_PTR(QTimer,this);
        connect(m_upIdleTimer.get(), SIGNAL(timeout()), SLOT(OnIdle()) );
        m_upIdleTimer->start(miliSeconds);
    }
}

void GmMessageBox::SetButtonVisible(int buttonType)
{
    for(int loop = 0; loop < (int)m_allButtons.size(); ++loop)
    {
        int loopButtonType = (1 << loop);
        bool isVisible = ((loopButtonType & buttonType) != 0);
        m_allButtons[loop]->setHidden(!isVisible);
    }
}

void GmMessageBox::OnButtonClick()
{
    for(int loop = 0; loop < (int)m_allButtons.size(); ++loop)
    {
        if(sender() == m_allButtons[loop])
        {
            m_clickedButton = (gcmp::UiCommonDialog::ButtonType)(1 << loop);
            break;
        }
    }
    DBG_WARN_UNLESS(m_clickedButton != gcmp::UiCommonDialog::ButtonType::Invalid, L"未能识别按钮",L"GDMPLab",L"2024-03-30");

    QDialog::accept();
}

void GmMessageBox::OnIdle()
{
    QDialog::accept();
}

QPushButton* GmMessageBox::GetButton(int idx)
{
    if(idx > (int)m_allButtons.size())
        return nullptr;

    return m_allButtons[idx];
}

bool GmMessageBox::eventFilter(QObject *watched, QEvent *event)
{
    if (QKeyEvent *pEvent = dynamic_cast<QKeyEvent*>(event))
    {
        if (pEvent->key() == (int)Qt::Key::Key_Enter || pEvent->key() == (int)Qt::Key::Key_Return)
        {
            QWidget *pWidget = QWidget::focusWidget();
            bool isFind = false;
            for (int loop = 0; loop < (int)m_allButtons.size() - 1; ++loop)
            {
                if (m_allButtons[loop] == pWidget)
                {
                    m_clickedButton = (gcmp::UiCommonDialog::ButtonType)(1 << loop);
                    isFind = true;
                    break;
                }
            }
            if (!isFind)
            {
                for (int loop = 0; loop < (int)m_allButtons.size() - 1; ++loop)
                {
                    if (m_allButtons[loop]->isVisible() == false)
                    {
                        continue;
                    }
                    m_clickedButton = (gcmp::UiCommonDialog::ButtonType)(1 << loop);
                    break;
                }
            }
           
        }
    }
    return QDialog::eventFilter(watched, event);
}

void GmMessageBox::onMoveRequested(const QPoint& cursorPos, const QPoint& offset)
{
    Q_UNUSED(cursorPos);
    move(pos() + offset);
}

